var web = require("../../utils/buildingInfoWeb.js");
Page({
  data: {
    // 是否模拟开盘
    // isMoni: my.getStorageSync({ key: 'isMoni' }).data,

    taskName: "",
    buildLocation: "",
    preSaleSum: "",
    developerName: "",
    selStartTime: "",

    buttonText: "选房告知书",
    // 是否已经同意选房告知书
    isNote: false,

    // 选房告知书对话框是否打开
    noteOpened: false,
    noteText: "",

    // 是否可以同意选房告知书
    isCanNote: false,
    buttonBackground: "rgba(157, 157, 157, 1)",
    // 为了实现每次启动倒计时，上次计时如果未到0，依然结束上次重置为30秒开始计时
    lastNoteId: 0,

    // 授权协议书对话框是否打开
    agreementOpened: false,
    // 是否选中同意协议
    check: false,
    // 是否可以点击获取验证码按钮
    isCanGetCode: true,
    // 授权协议书对话框的验证码
    codeInputValue: "",
    // 是否能第二次点击
    secondClick: true,

    taskType: "房"
  },
  onLoad() {
    var taskType = "房";
    if (my.getStorageSync({ key: 'taskType' }).data == "车位") {
      taskType = "车位";
      my.setNavigationBar({ title: "车位简介" });
    }
    var taskInfo = my.getStorageSync({ key: 'taskInfo' }).data;
    this.setData({
      taskName: taskInfo.taskName,
      buildLocation: taskInfo.buildLocation,
      preSaleSum: taskInfo.preSaleSum,
      developerName: taskInfo.developerName,
      selStartTime: taskInfo.selStartTime,
      buttonText: "选" + taskType + "告知书",
      taskType: taskType
    });
    web.client.getHouseNotice(this);
  },
  note() {
    var that = this;
    // 还没同意选房告知书
    if (!this.data.isNote) {
      this.setData({ noteOpened: true });
      // 如果没有曾经倒计时满30秒过，则需要倒计时
      if (!this.data.isCanNote) {
        // 开始倒计时 TODO 30
        web.client.countDown(10, this);
      }
    } else {
      // 已同意选房告知书，则打开授权协议书
      that.setData({ agreementOpened: true });
    }
  },
  noteYes() {
    if (this.data.isCanNote) {
      this.setData({
        noteOpened: false,
        buttonText: "签到进入选" + this.data.taskType + "大厅",
        // 已经同意选房告知书
        isNote: true
      });
    }
  },
  noteNo() {
    this.setData({
      noteOpened: false,
      lastNoteId: this.data.lastNoteId + 1
    });
  },
  checkboxOnChange(e) {
    if (e.detail.value == "选中") {
      this.setData({ check: true });
    } else {
      this.setData({ check: false });
    }
  },
  codeInput(e) {
    this.setData({ codeInputValue: e.detail.value });
  },
  getCode() {
    if (this.data.check) {
      // 开始倒计时
      web.client.countDown(60, this);
      web.client.getAgreementCode(this);
    } else {
      my.showToast({ content: "请同意授权协议书", duration: 2000 });
    }
  },
  submit() {
    if (!this.data.check) {
      my.showToast({ content: "请同意授权协议书", duration: 2000 });
    } else if (this.data.codeInputValue == null || this.data.codeInputValue == "") {
      my.showToast({ content: "验证码不能为空", duration: 2000 });
    } else {
      web.client.submitAgreement(this.data.codeInputValue, this);
    }
  },
  onShareAppMessage() {
    return {
      title: '公证 e 选房',
      desc: '提供在线选房服务',
      path: 'pages/login/login'
    };
  }
});
